let tpl = require('./wechat/tpl');
let WeChat = require('./wechat/wechat');
let config = require('./wechat/config');
let menu = require('./wechat/menu');

let wechatApi = new WeChat(config.wechat);
// wechatApi.deleteMenu().then(()=> {
//   return wechatApi.createMenu(menu)
// }).then(function (data) {
//   console.log(data)
// })
exports.reply = async function (result) {
    console.log('result', result.Content)
    this.status = 200;
    this.type = 'application/xml';
    let now = new Date().getTime();
    if (result.MsgType === 'event') {
      if (result.Event === 'subscribe') {
        this.body = tpl.text(result,now, "nihao")
      }
      else if(result.Event === 'CLICK' && result.EventKey === 'CLICK_MUSIC') {
        this.body = tpl.text(result,now, "点击了今日歌曲菜单")
      }
      else if(result.Event === 'scancode_push' && result.EventKey === 'scan') {
        this.body = tpl.text(result,now, `${result.ScanCodeInfo.ScanResult}`)
      }
      else if(result.Event === 'pic_photo_or_album') {
        this.body = tpl.text(result,now, `123`)
      }
      else if(result.Event === 'location_select') {
        this.body = tpl.text(result,now, `123`)
      }
    }
    else if (result.MsgType === 'text') {
      let now = parseInt((new Date().getTime()) / 1000);
      if(result.Content === '1') {
        let replyData = await wechatApi.batchMaterial({
          type: 'image',
          count: 3,
          offset: 0
        });
        return tpl.img(result, now, replyData.item[0].media_id);
      }
      else if(result.Content === '2') {
        let replyData = await wechatApi.batchMaterial({
          type: 'video',
          count: 3,
          offset: 0
        });
        return tpl.video(result, now, replyData.item[0].media_id,{
          title: "video",
          description: 'A brand new Trip'
        });
      }
      else if(result.Content === '3') {
        await wechatApi.upLoadMaterial('video', __dirname + '/material/1.mp4',{
          type: 'video',
          description: '{"title": "jaja","introduction": "never say never" }'
        }).then(data => {
          return tpl.video(result, now, data.media_id);
        })
      }
      else if(result.Content === '4') {
        let data = await wechatApi.upLoadMaterial('image', __dirname + '/material/3.jpg',{})
        let media = {
          articles:[{
            title: 'hah',
            thumb_media_id: data.media_id,
            author: 'Anthony',
            digest: 'no',
            show_cover_pic: 1,
            content: 'nononon',
            content_source_url: 'www.baidu.com'
          }]
        };
        data = await wechatApi.upLoadMaterial('news', media,{});
        data = await wechatApi.fetchMaterial(data.media_id, 'news', {});

        let item = data.news_item;
        let news = [];
        item.forEach(it => {
          news.push({
            title: it.title,
            description: it.digest,
            picUrl: it.thumb_url,
            url: it.url
          })
        });
        return tpl.link(result,now,news);
      }
      else if(result.Content === '5') {
        let data = await wechatApi.batchMaterial({
          type: 'video',
          count: 3,
          offset: 0
        });
        return tpl.text(result,now, "获取到了素材列表的内容")
      }
      else if(result.Content === '6') {
        let data = await wechatApi.countMaterial();
        return tpl.text(result,now, `永久素材 video：${data.video_count}，image: ${data.image_count},news: ${data.news_count},vocie: ${data.voice_count}`)
      }
      else if(result.Content === '7') {
        let dataCreate = await wechatApi.createGroup(`${Date.now()}`);
        let dataGet = await wechatApi.getGroup();
        return tpl.text(result,now, `创建标签 create: ${dataCreate.tag.id}, get: ${dataGet.tags[dataGet.tags.length - 1].name}`)
      }
      else if(result.Content === '8') {
        let data = await wechatApi.getUsersGroup(100);
        return tpl.text(result, now, `获取标签下粉丝列表`)
      }
      else if(result.Content === '9') {
        let data = await wechatApi.getUserInfo(result.FromUserName);
        return tpl.text(result,now, `获取用户信息`)
      }
      else if(result.Content === '10') {
        let data = await wechatApi.getUsersList();
        return tpl.text(result,now, `获取用户列表`)
      }
      else if(result.Content === '11') {
        let data = await wechatApi.sendAll('text', {
          content: 'hello'
        });
        return tpl.text(result,now, `群发`)
      }
      else if(result.Content === '12') {
        let data = await wechatApi.semanticData({
          query:"查一下明天从北京到上海的南航机票",
          city:"北京",
          category: "flight,hotel",
          appid:result.ToUserName,
        });
        return tpl.text(result,now, `${data.semantic.details.answer}`)
      }
      else {
        let url = 'http://zhangpengzuishuaiqi.club/search' + '?title=' + result.Content;
        return tpl.link(result,now,[
          {
            title: result.Content,
            description: '搜索到的相关电影简介',
            picUrl: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545973374183&di=b26ede6039d7152e9e08f0628e8d18cf&imgtype=0&src=http%3A%2F%2Fpic2.orsoon.com%2F2016%2F0914%2F20160914112533658.png',
            url: url
          }
        ]);
      }
    }
    else if (result.MsgType === 'location') {
      this.body = tpl.text(result,now, "获取到了位置信息")
    }
    else if (result.MsgType === 'image') {
      this.body = tpl.text(result,now, "获取到了图片")
    }
    else if (result.MsgType === 'voice') {
      this.body = tpl.text(result,now, "获取到了语音信息")
    }
    else {
      this.body = tpl.text(result,now, "helloWorld123")
    }
};
